package pe.com.certicom.ittsa.framework.dat;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import pe.com.certicom.ittsa.framework.dat.Conexion;

public class Conexion implements ParametrosConexion {
	private static final Log log = LogFactory.getLog(Conexion.class);
	Connection connection;

	public Conexion() {

	}

	public Connection ObtenerConnection() throws Exception {
		connection = getConnectionJDBC();
		return connection;
	}

	// SQL Server 2005
	public Connection getConnectionJDBC() throws Exception {
		try {
			Class.forName(Driver_SQL);
			// String url="jdbc:sqlserver://"+Server+":"+Puerto+";databaseName="+Name_BD;
			 String url="jdbc:sqlserver://"+ Server +":"+ Puerto +";databaseName="+Name_BD;
			//String url = "jdbc:odbc:" + Server;
			connection = DriverManager.getConnection(url, User_BD, Pass_BD);
			log.info("url ==> " + url);
		} catch (Exception e) {
			log.info("Error ==> " + e);
		}

		return connection;
	}

	// Posgress
	public Connection conectarPosgressGPS() throws SQLException {
		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			// Class.forName("net.sourceforge.jtds.jdbc.Driver");
			Class.forName("org.postgresql.Driver");
			//String url = "jdbc:postgresql://localhost:5432/gps?user=postgres&password=postgres";
			 //String url ="jdbc:postgresql://10.162.170.234:5432/gps?user=postgres&password=25836457gps";
			String url ="jdbc:postgresql://190.116.34.62:5432/gps?user=postgres&password=25836457gps";
			return DriverManager.getConnection(url);
		} catch (SQLException e) {
			System.out.println("No nos pudimos conectar A Postgres-->>"
					+ e.getMessage());
			return null;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("No nos pudimos conectar A Postgres-->>"
					+ e.getMessage());
			return null;
		} finally {
		}
	}

	public void desconectar(ResultSet rst, PreparedStatement pst, Connection con) {
		cerrarPS(rst);
		cerrarPS(pst);
		cerrarPS(con);
	}

	public static void cerrarPS(ResultSet rst) {
		try {
			if (rst != null) {
				rst.close();
				rst = null;
			}
		} catch (Exception e) {
		}
	}

	public static void cerrarPS(Statement stm) {
		try {
			if (stm != null) {
				stm.close();
				stm = null;
			}
		} catch (Exception e) {
		}
	}

	public static void cerrarPS(Connection con) {
		try {
			if (con != null) {
				con.close();
				con = null;
			}
		} catch (Exception e) {
		}
	}

	public static void main(String args1[]) {
		Conexion con = new Conexion();
		try {
			con.ObtenerConnection();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
